<?php


    // Author: Tomasz Durka
    // Date: 6th February 2007
    // Prints out a HTML/Ajax remote form
    
    
  function form_remote ($params) {  
    $module = module;
    $action = action;
    $parameter = false;
    $method = 'post';
    $name = '';
    $id = '';
    $class = '';
    $style = '';
    $before = '';
    $after = '';
    $enctype = '';
    $confirm = '';
    $insertion = '';
    $success = '';
    $location = false;
    if (!isset($params['module']) && !isset($params['action']) && !isset($params['parameter'])) {
      $path_to = '';
    }
    
    foreach($params as $_key => $_val) {
      switch ($_key) {
        case 'name':
        case 'id':
        case 'class':
        case 'style':
        case 'enctype':
          $$_key = ' ' . $_key . '="' . $_val . '"';
          break;
        case 'confirm':
          $confirm = "if (!confirm('{$_val}')) return false;";
          break;
        case 'before':
          $$_key = $_val;
          break;
        case 'after':
          $$_key = $_val;
          break;
        case 'update':
          $update = "\$({$_val}), ";
          $parameters = "parameters:Form.serialize(this), ";
          break;
        case 'insertion':
          $insertion = ", insertion:Insertion.{$_val}";
          break;
        case 'success':
          $success = ", onSuccess: function() {{$_val}}";
          break;
        case 'location':
        case 'url':
            $location = '/' . $_val;
          break;
        default:
          $$_key = $_val;
          break;
      }
    }
    
    if ($location) {
      $path_to = $location;
    } else {
      $path_to = isset($path_to) ? $path_to : '' . '/' . $module . '/' . $action . '/' . $parameter;
    }
    if ($update != '') {
      $ajax_begin = 'Updater';
    } else {
      $ajax_begin = 'Request';
    }
    $ajax_call = "new Ajax.{$ajax_begin}({$update}'{$path_to}', {{$parameters}evalScripts:true, asynchronous:true{$insertion}{$success}});";
    return "<form action=\"{$path_to}\" method=\"{$method}\" onSubmit=\"{$confirm}{$before};{$ajax_call}{$after};return false;\"{$name}{$id}{$class}{$style}{$enctype}>";
  }

?>
